package com.android.settings.wifi.tether.entitlement_setting_client;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

/**
 * @Descpition:
 * @Author wpc
 * @Date 2023/7/22 12:33
 **/
public class MobileHotspotClientTest {
    private static MobileHotspotClientTest mobileHotspotClientTest = null;
    private Context context;
    private CarrierConfig carrierConfig;
    private static Messenger serviceMessenger;
    private static Messenger messenger = new Messenger(new MobileHotspotClientTest.serviceMessengerHandler());

    public MobileHotspotClientTest(Context context){
        this.context = context;
    }

    void initMhsClientInstance() {
        carrierConfig = CarrierConfig.getInstance();
    }

    private static ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            slog("2、After the service is successfully bound");
            serviceMessenger = new Messenger(service);
            notifyServiceStartMhsEntitlement();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            slog("2、service is disConnected");
        }
    };


    public void startBindMhsService() {
        initMhsClientInstance();
        String simCarrier = carrierConfig.getSimCardInfo();
        slog("1、startBindMhsService: simCarrier == " + simCarrier);
        Intent intent = new Intent();
        intent.setPackage(context.getPackageName());
        switch (simCarrier) {
            case "ATT":
                intent.setAction(MhsConstants.ATT_ACTION);
                break;
            case "VZW":
                intent.setAction(MhsConstants.VZW_ACTION);
            case "TM":
                intent.setAction(MhsConstants.TM_ACTION);
                break;
            case "DS":
                intent.setAction(MhsConstants.DS_ACTION);
                break;
        }
        context.bindService(intent, connection, BIND_AUTO_CREATE);
    }

    public void stopBindMhsService() {
        if (messenger != null || serviceMessenger != null){
            messenger = null;
            serviceMessenger = null;
        }
        try {
            slog("stopBindMhsService");
            context.unbindService(connection);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private static class serviceMessengerHandler extends Handler {
        public serviceMessengerHandler() {
            super();
        }
    }

    public static void notifyServiceStartMhsEntitlement(){
        slog("3、The client notifies the server of mhs authentication");
        Message msg = Message.obtain();
        Bundle bundle = new Bundle();
        msg.what = MhsConstants.START_HOTSPOT_TETHER;
        msg.replyTo = messenger;
        msg.setData(bundle);
        try {
            serviceMessenger.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void slog(String log) {
        Log.d("mobileHotspotClientTest_entitlement", log);
    }
}
